/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.util.List;
import javax.sql.DataSource;
import limousine.report.PrintConfirm;
import limousine.vo.Booking;
import limousine.vo.BookingBean;
import limousine.vo.BookingDetail;
import limousine.vo.BookingInfo;
import limousine.vo.CustomerInfo;
import org.jsrpc.servlet.SessionBean;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.workforce.util.Utils;
import org.workforce.vo.LabelValue;

/**
 *
 * @author krisada
 */
public class BookingBeanManager extends SessionBean {

    private int count = 0;
    private Service bookingService = null;
    private Service bookingDetailService = null;
    private DataSource dataSource = null;
    private BookingSearcher bookingSearcher = null;
    private CustomerSearcher customerSearcher = null;

    public void init() {
        if (bookingService == null) {
            bookingService = (Service) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("bookingService");
        }
        if (bookingDetailService == null) {
            bookingDetailService = (Service) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("bookingDetailService");
        }
        if (dataSource == null) {
            dataSource = (DataSource) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("dataSource");
        }
        if (bookingSearcher == null) {
            bookingSearcher = (BookingSearcher) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("bookingSearcher");
        }
    }

    public boolean addBooking(BookingBean bean) {
        init();
        bean.setStatus(2);
        Booking b = new Booking();
        BookingDetail d = new BookingDetail();
        bean.setBookingId(Utils.generateId());
        b.setBookingId(bean.getBookingId());
        d.setBookingId(bean.getBookingId());
        b.setCustomerId(bean.getCustomerId());
        b.setCreateBy(bean.getCreateBy());
        b.setUpdateBy(bean.getUpdateBy());
        b.setStatus(bean.getStatus());
        d.setStatus(bean.getStatus());
        d.setCarId(bean.getCarId());
        d.setDriverId(bean.getDriverId());
        //System.out.println("Driver:"+ d.getDriverId());
        d.setTransportType(bean.getTransportType());
        d.setBookingType(bean.getBookingType());
        //System.out.println("booking type:"+ d.getBookingType());
        d.setTargetType(bean.getTargetType());
        d.setFromArea(bean.getFromArea());
        d.setToArea(bean.getToArea());
        d.setGuest(bean.getGuest());
        d.setAtTime(bean.getAtTime());
        d.setFlight(bean.getFlight());
        d.setRoom(bean.getRoom());
        d.setPax(bean.getPax());
        //System.out.println("Female1:"+ bean.getFemale());
        d.setFemale(bean.getFemale());
        d.setMale(bean.getMale());
        d.setChild(bean.getChild());
        d.setCarReuestType(bean.getCarReuestType());
        d.setFromDate(bean.getFromDate());
        d.setToDate(bean.getToDate());
        d.setVcNo(bean.getVcNo());
        d.setBookingBy(bean.getBookingBy());
        d.setDiscountRate(bean.getDiscountRate());
        d.setRemark(bean.getRemark());
        d.setTargetType(bean.getTripType());
        d.setPaymentType(bean.getPaymentType());
        d.setRateCharge(bean.getRateCharge());
        d.setFastTrack(bean.getFastTrack());
        d.setVerifyStatus(bean.getVerifyStatus());
        d.setDropOffTime(bean.getDropOffTime());
        if (bookingService.save(b)) {
            if (bookingDetailService.save(d)) {
                return true;
            }
        }
        return false;
    }

    public boolean editBooking(BookingBean bean) {
        init();
        Booking b = new Booking();
        BookingDetail d = new BookingDetail();
        b.setBookingId(bean.getBookingId());
        d.setBookingId(bean.getBookingId());
        b.setCustomerId(bean.getCustomerId());
        b.setCreateBy(bean.getCreateBy());
        b.setUpdateBy(bean.getUpdateBy());
        b.setStatus(bean.getStatus());
        d.setStatus(bean.getStatus());
        d.setBookingId(bean.getBookingId());
        d.setCarId(bean.getCarId());
        d.setDriverId(bean.getDriverId());
        d.setTransportType(bean.getTransportType());
        d.setBookingType(bean.getBookingType());
        d.setTargetType(bean.getTargetType());
        d.setFromArea(bean.getFromArea());
        d.setToArea(bean.getToArea());
        d.setGuest(bean.getGuest());
        d.setAtTime(bean.getAtTime());
        d.setFlight(bean.getFlight());
        d.setRoom(bean.getRoom());
        d.setPax(bean.getPax());
        d.setFemale(bean.getFemale());
        d.setMale(bean.getMale());
        d.setChild(bean.getChild());
        d.setCarReuestType(bean.getCarReuestType());
        d.setFromDate(bean.getFromDate());
        d.setToDate(bean.getToDate());
        d.setVcNo(bean.getVcNo());
        d.setBookingBy(bean.getBookingBy());
        d.setDiscountRate(bean.getDiscountRate());
        d.setRemark(bean.getRemark());
        d.setTargetType(bean.getTripType());
        d.setPaymentType(bean.getPaymentType());
        d.setRateCharge(bean.getRateCharge());
        d.setFastTrack(bean.getFastTrack());
        d.setVerifyStatus(bean.getVerifyStatus());
        d.setDropOffTime(bean.getDropOffTime());
        if (bookingService.update(b)) {
            if (bookingDetailService.update(d)) {
                if( b.getStatus() == 3 ) {
                    //sendMailConfirm("sangasnit@gmail.com", "sangsanit@yahoo.com", 
                     //       "Confirmation from Link Intertravel.", "Please read mail confirmation in PDF file.");
                }
                return true;
            }
        }
        return false;
    }
    
    private void sendMailConfirm(String from, String to, String subject, String text) {
        SendMailBaseApp send = new SendMailBaseApp();
        send.setContent(printConfirm());
        send.setFileName("INV"+Utils.getDate()+".pdf");
        send.setFrom(from);
        send.setTo(to);
        send.setSubject(subject);
        send.setText(text);
        (new Thread(send)).start();
    }

    private byte[] printConfirm() {
        byte[] bytes = null;
        try {
           PrintConfirm print = new PrintConfirm();
           bytes = print.printConfirm(this.getServletContext());
            return bytes;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bytes;
    }

    public boolean removeBooking(BookingBean bean) {
        init();
        if (bookingService.remove(bean.getBookingId())) {
            if (bookingDetailService.remove(bean.getBookingId())) {
                return true;
            }
        }
        return false;
    }

    public BookingBean findBooking(String uid, String id) {
        init();
        BookingBean bean = new BookingBean();
        Booking b = (Booking) bookingService.findById(uid, id);
        BookingDetail d = (BookingDetail) bookingDetailService.findById(id);
        bean.setBookingId(b.getBookingId());
        bean.setCustomerId(b.getCustomerId());
        bean.setCreateBy(b.getCreateBy());
        bean.setCreateDate(b.getCreateDate());
        bean.setUpdateBy(b.getUpdateBy());
        bean.setUpdateDate(b.getUpdateDate());
        bean.setStatus(b.getStatus());
        bean.setLock(b.getLock());
        bean.setTimeOut(b.getTimeout());
        bean.setCarId(d.getCarId());
        bean.setDriverId(d.getDriverId());
        bean.setTransportType(d.getTransportType());
        bean.setBookingType(d.getBookingType());
        bean.setTargetType(d.getTargetType());
        bean.setFromArea(d.getFromArea());
        bean.setToArea(d.getToArea());
        bean.setGuest(d.getGuest());
        bean.setAtTime(d.getAtTime());
        bean.setFlight(d.getFlight());
        bean.setRoom(d.getRoom());
        bean.setPax(d.getPax());
        bean.setCarReuestType(d.getCarReuestType());
        bean.setFromDate(d.getFromDate());
        bean.setToDate(d.getToDate());
        bean.setVcNo(d.getVcNo());
        bean.setBookingBy(d.getBookingBy());
        bean.setFemale(d.getFemale());
        bean.setMale(d.getMale());
        bean.setChild(d.getChild());
        bean.setDiscountRate(d.getDiscountRate());
        bean.setRemark(d.getRemark());
        bean.setTargetType(d.getTripType());
        bean.setPaymentType(d.getPaymentType());
        bean.setRateCharge(d.getRateCharge());
        bean.setFastTrack(d.getFastTrack());
        bean.setVerifyStatus(d.getVerifyStatus());
        bean.setDropOffTime(d.getDropOffTime());
        return bean;
    }

    public List<BookingInfo> searchBooking(int offset, String param[]) {
        init();
        List list = bookingService.search(offset, param);
        return list;
    }

    public List<BookingInfo> queryBooking(int offset) {
        init();
        List list = bookingSearcher.query(offset);
        return list;
    }

    public List<BookingInfo> queryBookingByDate(int offset, String sdate) {
        init();
        List list = bookingSearcher.findByDate(offset, sdate);
        return list;
    }

    public List<BookingInfo> queryBookingByCustomer(int offset, String customerId, String sdate) {
        init();
        List list = bookingSearcher.findByCustomer(offset, customerId, sdate);
        return list;
    }

    public List<LabelValue> getOption(String sName) {
        init();
        SearchLookup search = new SearchLookup();
        search.setDataSource(dataSource);
        List<LabelValue> list = search.find(new Object[]{sName});
        return list;
    }

    public List<LabelValue> getCarOption() {
        init();
        SearchCar search = new SearchCar();
        search.setDataSource(dataSource);
        List<LabelValue> list = search.find(null);
        return list;
    }

    public List<LabelValue> getCarRequestOption() {
        init();
        SearchCarRequest search = new SearchCarRequest();
        search.setDataSource(dataSource);
        List<LabelValue> list = search.find(null);
        return list;
    }

    public List<LabelValue> getDriverOption() {
        init();
        SearchDriver search = new SearchDriver();
        search.setDataSource(dataSource);
        List<LabelValue> list = search.find(null);
        return list;
    }

    public List<CustomerInfo> findCustomer(int offset, String name) {
        if (customerSearcher == null) {
            customerSearcher = (CustomerSearcher) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("customerSearcher");
        }
        return customerSearcher.search(offset, name);
    }

    public CustomerInfo findCustomerById(String id) {
        if (customerSearcher == null) {
            customerSearcher = (CustomerSearcher) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("customerSearcher");
        }
        return customerSearcher.findById(id);
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
